import LogTool from '../com/LogTool/LogTool';

class HttpUtil {
    baseUrl: string;

    constructor(baseUrl?: string) {
        this.baseUrl = baseUrl || '';
    }

    async getJson(endpoint: string, params = {}, headers = {}) {
        try {
            const url = new URL(endpoint, this.baseUrl);
            url.search = new URLSearchParams(params).toString();
            const response = await fetch(url, {
                method: 'GET',
                headers: {
                    ...headers
                }
            });

            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
            return await response.json();
        } catch (err: any) {
            const logger = await LogTool.getLogger();
            logger.warn('HttpUtil GET', {message: err + err.stack});
        }
    }

    async post(endpoint: string, body = {}, headers = {}) {
        try {
            const url = new URL(endpoint, this.baseUrl);
            const response = await fetch(url, {
                method: 'POST',
                headers: {
                    'content-type': 'application/json',
                    ...headers
                },
                body: JSON.stringify(body)
            });

            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }

            return await response.json();
        } catch (err: any) {
            const logger = await LogTool.getLogger();
            logger.warn('HttpUtil POST', {message: err + err.stack});
        }
    }

    async put(endpoint: string, body = {}, headers = {}) {
        try {
            const url = new URL(endpoint, this.baseUrl);

            const response = await fetch(url, {
                method: 'PUT',
                headers: {
                    'content-type': 'application/json',
                    ...headers
                },
                body: JSON.stringify(body)
            });

            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }

            return await response.json();
        } catch (err: any) {
            const logger = await LogTool.getLogger();
            logger.warn('HttpUtil PUT', {message: err + err.stack});
        }
    }

    async delete(endpoint: string, params = {}, headers = {}) {
        try {
            const url = new URL(endpoint, this.baseUrl);
            url.search = new URLSearchParams(params).toString();

            const response = await fetch(url, {
                method: 'DELETE',
                headers: {
                    ...headers
                }
            });

            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }

            return await response.json();
        } catch (err: any) {
            const logger = await LogTool.getLogger();
            logger.warn('HttpUtil DELETE', {message: err + err.stack});
        }
    }
}

export default HttpUtil;